package com.ruoyi.web.controller.majors;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Major;
import com.ruoyi.system.service.IMajorService1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author rayfoo
 */
@RestController
@RequestMapping("/majors1/majors1")
public class MajorsController1 extends BaseController {
    @Autowired
    private IMajorService1 majorService;

    /**
     * 获取岗位列表
     */
    @PreAuthorize("@ss.hasPermi('system:post:list')")
    @GetMapping("/list")
    public TableDataInfo list(Major major)
    {
        startPage();
        List<Major> list = majorService.selectPostList(major);
        return getDataTable(list);
    }

    /**
     * 根据ID获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:post:query')")
    @GetMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success(majorService.selectPostById(id));
    }

    /**
     * 添加专业
     */
    @PreAuthorize("@ss.hasPermi('system:post:add')")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Major major)
    {
        if (!majorService.checkPostNameUnique(major))
        {
            return error("添加专业'" + major.getMajorName() + "'失败，专业名称已存在");
        }
        else if (!majorService.checkPostCodeUnique(major))
        {
            return error("添加专业'" + major.getId()+ "'失败，专业ID已存在");
        }
        major.setCreator(getUsername());

        return toAjax(majorService.insertPost(major));
    }


}